package com.qubuyer.business.good.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.view.LayoutInflater;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.blankj.utilcode.util.ConvertUtils;
import com.blankj.utilcode.util.SizeUtils;
import com.qubuyer.R;
import com.qubuyer.base.activity.BaseActivity;
import com.qubuyer.bean.good.GoodAssessEntity;
import com.qubuyer.bean.good.GoodCommentEntity;
import com.qubuyer.repository.entitys.HomeGoodEntity;
import com.qubuyer.bean.shopcart.ShopCartGoodEntity;
import com.qubuyer.business.good.adapter.GoodsParamsAdapter;

import java.util.List;

/**
 * @author Susong
 * @date 创建时间2019/3/24
 * @description 商品购买须知页
 */
public class GoodParamsPage extends GoodDetailBaseViewPage implements IGoodDetailView {
    //当前上下文
    private Context mContext;
    private View mView;
    //商品ID
    private String mGoodId;

    private GoodsParamsAdapter adapter;

    public GoodParamsPage(Context mContext, String goodId) {
        this.mContext = mContext;
        mGoodId = goodId;
    }

    public View getView() {
        if (mView == null) {
            mView = LayoutInflater.from(mContext).inflate(R.layout.layout_activity_goods_params_page, null);
            RecyclerView recyclerView = mView.findViewById(R.id.recyclerView);
            recyclerView.setLayoutManager(new LinearLayoutManager(mContext));
            Paint paint = new Paint();
            paint.setColor(Color.parseColor("#999999"));
            recyclerView.addItemDecoration(new RecyclerView.ItemDecoration() {
                private int dp1 = ConvertUtils.dp2px(1f);
                private Rect mBounds = new Rect();
                @Override
                public void onDraw(@NonNull Canvas c, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
                    super.onDraw(c, parent, state);
                    c.drawRect(0, 0, parent.getWidth(), dp1, paint);
                    c.save();
                    int left;
                    int right;
                    if (parent.getClipToPadding()) {
                        left = parent.getPaddingLeft();
                        right = parent.getWidth() - parent.getPaddingRight();
                        c.clipRect(left, parent.getPaddingTop(), right,parent.getHeight() - parent.getPaddingBottom());
                    } else {
                        left = 0;
                        right = parent.getWidth();
                    }

                    int childCount = parent.getChildCount();
                    for (int i =0;i<childCount;i++) {
                        View child = parent.getChildAt(i);
                        int position = parent.getChildAdapterPosition(child);
                        if (position >= 0) {
                            parent.getDecoratedBoundsWithMargins(child, mBounds);
                            int top = (int) (mBounds.top + child.getTranslationY());
                            int bottom = top + dp1;
                            c.drawRect(left , top, right, bottom, paint);
                        }
                        if (position == parent.getAdapter().getItemCount() - 1) {
                            parent.getDecoratedBoundsWithMargins(child, mBounds);
                            int top = (int) (mBounds.bottom + child.getTranslationY());
                            int bottom = top + dp1;
                            c.drawRect(left , top, right , bottom, paint);
                        }
                    }
                    c.restore();
                }

                @Override
                public void getItemOffsets(@NonNull Rect outRect, @NonNull View view, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
                    super.getItemOffsets(outRect, view, parent, state);
                    int position = parent.getChildAdapterPosition(view);
                    outRect.top = dp1;
                    if (position == parent.getAdapter().getItemCount() - 1) {
                        outRect.bottom = dp1;
                    }
                }
            });
            recyclerView.setPadding(0, SizeUtils.dp2px(43f),0,0);
            adapter = new GoodsParamsAdapter();
            recyclerView.setAdapter(adapter);
        }
        return mView;
    }

    @Override
    public void showLoading() {
        ((BaseActivity) mContext).showLoading();
    }

    @Override
    public void hideLoading() {
        ((BaseActivity) mContext).hideLoading();
    }

    @Override
    public void doResponseError(int code, String message) {
        ((BaseActivity) mContext).doResponseError(code, message);
    }

    @Override
    public void destory() {
    }

    @Override
    public void loadData() {
    }

    @Override
    public void onShowGoodDetailToView(HomeGoodEntity entity) {
        adapter.setDataList(entity.getGoods_attr());
        adapter.addDataList(entity.getGoods_param());
    }

    @Override
    public void onShowShopCartListDataToView(List<ShopCartGoodEntity> list) {

    }

    @Override
    public void onShowCollectGoodResultToView(boolean result) {

    }

    @Override
    public void onShowAddGoodToCartResultToView(boolean result) {

    }

    @Override
    public void onShowGoodCommentListToView(List<GoodCommentEntity> list) {
    }

    @Override
    public void onShowGoodAssessToView(GoodAssessEntity entity) {

    }
}


